# -*- coding: utf-8 -*-
"""
Created on Wed Apr  6 16:57:12 2022

@author: mizhi
"""
import pandas as pd
import sys
import os
import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.feature_selection import VarianceThreshold
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
from sklearn.metrics import mean_squared_error
def data_handle(item,n):
   timeType = item[0]
   time = []
   #print(item)
   for i in range(1,12):
       if i<len(item):
           time.append(float(item[i]))
       else:
           time.append(0)
   #print(time)
   setData = {}
   for i in range(1,n):
       e = '1.'+str(i)
       setData[e] = time[i-1]
   #print(setData)
   listData = []
   listData.append(setData)
   #print(listData)
   data = pd.DataFrame(listData)
   #print(data)
   return data       
def liner(item):
    path = "E:\大四下\毕业设计\AguaH.csv"
    data = pd.read_csv(path)
    data = data.iloc[:, 5:]
    data.dropna(inplace=True)
    transfer = VarianceThreshold(threshold=10)
    data_new = transfer.fit_transform(data)
    temp = []
    i = item
    """
    while i < 72:
        temp.append(i)
        i+=12
        """
    x = data.iloc[:,0:item]
    #print(x)
    y = data.iloc[:,item]
    #print(y)
    
    x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4）预估器
    estimator = Ridge(alpha=0.5, max_iter=10000)
    estimator.fit(x_train, y_train)
    joblib.dump(estimator, "month.pkl")
    y_predict = estimator.predict(x_test)
    error = mean_squared_error(y_test, y_predict)
    # 5）得出模型
    #print("岭回归-权重系数为：\n", estimator.coef_)
    #print("岭回归-偏置为：\n", estimator.intercept_)

    # 6）模型评估
    return estimator,error   
if __name__ == "__main__":
    print("open")
    a = []
    for i in range(1, len(sys.argv)):
        a.append(sys.argv[i])
    month = int(a[0])
    data = data_handle(a,month)
    estimator,error = liner(month-1)
    predict = estimator.predict(data)
    print(predict[0])
    print(error)